package com.demo.base.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description 曳戈泰尔
 * @date 2023/3/22 9:53 PM
 */
@Component
@Slf4j
public class MailHelper {

    /** 注入Mail服务 */
    private JavaMailSender javaMailSender;

    @Autowired
    private void setMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    /** 配置文件中我的qq邮箱 */
    @Value("${spring.mail.username}")
    private String from;

    /** 设置发送者 */
    @Value("${spring.mail.username}")
    private String to;

    /**
     * 简单文本邮件
     *
     * @param content 内容
     */
    @Async
    public void sendSimpleMail(String content) {

        try {
            // 创建SimpleMailMessage对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 邮件发送人
            message.setFrom(from);
            // 邮件接收人
            message.setTo(to);
            // 邮件主题
            String subject = "AI表情包 疑似 DOS攻击";
            message.setSubject(subject);
            // 邮件内容
            message.setText(content);

            // 发送邮件
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("发送邮件发生异常，原因：{}", e.getMessage());
        }
    }

    /**
     * 简单文本邮件
     *
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    @Async
    public void sendSimpleMail(String to, String subject, String content) {

        try {
            // 创建SimpleMailMessage对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 邮件发送人
            message.setFrom(from);
            // 邮件接收人
            message.setTo(to);
            // 邮件主题
            message.setSubject(subject);
            // 邮件内容
            message.setText(content);

            // 发送邮件
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("发送邮件发生异常，原因：{}", e.getMessage());
        }
    }

    /**
     * html邮件
     *
     * @param to 收件人,多个时参数形式 ："xxx@xxx.com,xxx@xxx.com,xxx@xxx.com"
     * @param subject 主题
     * @param content 内容
     */
    @Async
    public void sendHtmlMail(String to, String subject, String content) {

        // 获取MimeMessage对象
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;

        try {
            messageHelper = new MimeMessageHelper(message, true);
            // 邮件发送人
            messageHelper.setFrom(from);
            // 邮件接收人,设置多个收件人地址
            InternetAddress[] internetAddressTo = InternetAddress.parse(to);
            messageHelper.setTo(internetAddressTo);
            // messageHelper.setTo(to);
            // 邮件主题
            message.setSubject(subject);
            // 邮件内容，html格式
            messageHelper.setText(content, true);

            // 发送
            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("发送邮件发生异常，原因：{}", e.getMessage());
        }
    }

    /**
     * 带附件的邮件
     *
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件
     */
    @Async
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {

        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);

            javaMailSender.send(message);
        } catch (Exception e) {
            log.error("发送邮件发生异常，原因：{}", e.getMessage());
        }
    }
}
